package org.yelongframework.spring.boot.autoconfigure;

import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 数据源信息打印配置
 * 
 * @since 1.0.0
 */
public class DataSourcePrintConfiguration implements ApplicationContextAware {

	private static final Logger log = LoggerFactory.getLogger(DataSourcePrintConfiguration.class);

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		Map<String, DataSourceProperties> dataSourcePropertiesMap = applicationContext
				.getBeansOfType(DataSourceProperties.class);
		if (MapUtils.isEmpty(dataSourcePropertiesMap)) {
			log.info("没有找到数据源配置");
			return;
		}
		for (Entry<String, DataSourceProperties> entry : dataSourcePropertiesMap.entrySet()) {
			String beanName = entry.getKey();
			log.info("datasource beanname : " + beanName);
			DataSourceProperties dataSourceProperties = entry.getValue();
			log.info("datasource name : " + dataSourceProperties.getName());
			log.info("datasource url : " + dataSourceProperties.getUrl());
			log.info("datasource username : " + dataSourceProperties.getUsername());
			log.info("datasource password : " + dataSourceProperties.getPassword());
		}
	}

}
